package com.parsifal.bluetoothdemo;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.parsifal.bluetoothdemo.listener.OnSocketReceiveListener;
import com.parsifal.bluetoothdemo.listener.OnSocketSendListener;
import com.parsifal.bluetoothdemo.iface.IBluetoothModel;
import com.parsifal.bluetoothdemo.socket.Client;
import com.parsifal.bluetoothdemo.socket.Server;

import java.util.Set;

/**
 * Created by yangming on 17-3-30.
 */
public class BluetoothModel implements IBluetoothModel {

    private static final String TAG = "BluetoothModel";

    private BluetoothAdapter mBluetoothAdapter = null;

    private Client mClient = null;

    private Server mServer = null;

    @Override
    public boolean init() {
        Log.e(TAG, "BluetoothModel init");
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean isEnabled() {
        return mBluetoothAdapter.isEnabled();
    }

    @Override
    public void createClient(OnSocketSendListener onSocketSendListener, OnSocketReceiveListener onSocketReceiveListener) {
        Log.e(TAG, "BluetoothModel createClient");
        mClient = new Client();
        mClient.setOnSendListener(onSocketSendListener);
        mClient.setOnReceiveListener(onSocketReceiveListener);
    }

    @Override
    public void createServer(OnSocketSendListener onSocketSendListener, OnSocketReceiveListener onSocketReceiveListener) {
        Log.e(TAG, "BluetoothModel createServer");
        mServer = new Server();
        mServer.setOnSendListener(onSocketSendListener);
        mServer.setOnReceiveListener(onSocketReceiveListener);
    }

    @Override
    public void connectServer(BluetoothDevice server) {
        Log.e(TAG, "BluetoothModel connectServer");
        mClient.connect(server);
    }

    @Override
    public void connectClient(BluetoothDevice client) {
        Log.e(TAG, "BluetoothModel connectClient");
        mServer.connect(mBluetoothAdapter, client);
    }

    @Override
    public void removeBond(BluetoothDevice device) {
        Log.e(TAG, "BluetoothModel removeBond");
        try {
            ClsUtils.removeBond(device.getClass(), device);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendToServer(String clientMsg) {
        Log.e(TAG, "BluetoothModel sendToServer");
        mClient.send(clientMsg);
    }

    @Override
    public void stopServer() {
        Log.e(TAG, "BluetoothModel stopServer");
        mServer.stopServer();
    }

    @Override
    public void stopClient() {
        Log.e(TAG, "BluetoothModel stopClient");
        mClient.stopClient();
    }

    @Override
    public void closeServerSocket(BluetoothDevice device) {
        Log.e(TAG, "BluetoothModel closeServerSocket");
        mServer.closeSocket(device);
    }

    @Override
    public void open(Activity activity) {
        Log.e(TAG, "BluetoothModel open");
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            activity.startActivityForResult(enableIntent, IBluetoothModel.REQUEST_ENABLE_BT);
        }
    }

    @Override
    public Set<BluetoothDevice> getBondedDevices() {
        Log.e(TAG, "BluetoothModel getBondedDevices");
        return mBluetoothAdapter.getBondedDevices();
    }

    @Override
    public void scan() {
        Log.e(TAG, "BluetoothModel scan");
        mBluetoothAdapter.startDiscovery();

//                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
//                    BluetoothLeScanner leScanner = mBluetoothAdapter.getBluetoothLeScanner();
//                    leScanner.startScan(new ScanCallback() {
//                        @Override
//                        public void onScanResult(int callbackType, ScanResult result) {
//                            super.onScanResult(callbackType, result);
//                            BluetoothDevice device = null;
//                            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
//                                device = result.getDevice();
//                            }
//                            if (!mDevices.contains(device)) {  //判断是否已经添加
//                                mDevices.add(device);
//                            }
//                            Log.e("test", "device " + device.toString());
//                        }
//
//                        @Override
//                        public void onBatchScanResults(List<ScanResult> results) {
//                            super.onBatchScanResults(results);
//                        }
//
//                        @Override
//                        public void onScanFailed(int errorCode) {
//                            super.onScanFailed(errorCode);
//                            Toast.makeText(MainActivity.this, "没有搜索到设备", Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                }
    }

    @Override
    public void ensureDiscoverable(Context context) {
        Log.e(TAG, "BluetoothModel ensureDiscoverable");
        if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            context.startActivity(discoverableIntent);
        }
    }

    @Override
    public void disable() {
        Log.e(TAG, "BluetoothModel disable");
        mBluetoothAdapter.disable();
    }
}
